package com.ngc.mall.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.AdminRoleDomain;
import com.ngc.mall.dto.RoleDTO;
import com.ngc.mall.service.IRoleService;
import com.ngc.mall.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * 角色
 *
 * @author: chenxiaolin
 * @date 2023/4/4 8:56
**/
@RestController
@RequestMapping("/role")
@Api(tags = "角色")
public class RoleController {
    @Resource
    private IRoleService service;

    @GetMapping("/list")
    @ApiOperation("获取所有角色")
    public ResponseResult<List<RoleVO>> selectAllRole(){
        return ResponseResult.success(service.selectAllRole());
    }


    @PostMapping("")
    @ApiOperation("新建角色")
    public ResponseResult<Boolean> createRole(@RequestBody RoleDTO roleDTO){
        return ResponseResult.success(service.createRole(roleDTO));
    }



    @PutMapping("/id/{id}")
    @ApiOperation("编辑角色信息")
    public ResponseResult<Boolean> updateRoleById(@PathVariable Long id,@RequestBody RoleDTO roleDTO){
        return ResponseResult.auto(service.updateRoleById(id,roleDTO));
    }


    @DeleteMapping("/id/{id}")
    @ApiOperation("删除角色")
    public Boolean deleteRoleById(@PathVariable Long id){
        return service.removeById(id);
    }


    @GetMapping("/id/{id}")
    @ApiOperation("查看角色信息")
    public ResponseResult<AdminRoleDomain> getRoleInfoById(@PathVariable Long id){
        return ResponseResult.success(service.getDetailById(id));
    }

    @PutMapping("/id/{id}/enable")
    @ApiOperation("启用角色")
    public ResponseResult<Boolean> updateStatusToEnabled(@PathVariable Long id){
        return ResponseResult.auto(service.update(Wrappers.<AdminRoleDomain>lambdaUpdate().eq(AdminRoleDomain::getId,id).set(AdminRoleDomain::getIsEnabled,true)));
    }


    @PutMapping("/id/{id}/disable")
    @ApiOperation("停用角色")
    public ResponseResult<Boolean> updateStatusToDisabled(@PathVariable Long id){
        return ResponseResult.auto(service.update(Wrappers.<AdminRoleDomain>lambdaUpdate().eq(AdminRoleDomain::getId,id).set(AdminRoleDomain::getIsEnabled,false)));
    }
}
